'use strict';
const db = uniCloud.database();
exports.main = async (event, context) => {
  //event为客户端上传的参数
 const fs = require("fs");
 const collection = db.collection('posts');
  console.log('event : ' + event)
  let list=[];
  for(let i=0; i<event.imgs.length; i++){
	  let path="post_imgs/"+event.post_id+i.toString()+".jpg";
	  list.push(path);
	  let src=event.imgs[i];
	 let result = await uniCloud.uploadFile({
		 cloudPath: path,
		 fileContent: new Buffer(src, 'base64')
	 });
  }
  let post={
	  "post_id":event.post_id,
	  "user_name":event.user_name,
	  "user_id":event.user_id,
	  "icon":event.icon,
	  "content":event.content,
	  "imgs":list,
	  "classify":event.classify,
	  "time":event.time,
	  "praise_count":0,
	  "comment_count":0
  }
	let res=await collection.add(post);
  //返回数据给客户端
  return {"status":2}
};
